package com.stardata.liangshisq.tcslmon.controller;

import com.stardata.liangshisq.tcslmon.annotation.IgnoreAuth;
import com.stardata.liangshisq.tcslmon.entity.DbiEmp;
import com.stardata.liangshisq.tcslmon.paramVo.LoginVo;
import com.stardata.liangshisq.tcslmon.service.DbiEmpService;
import com.stardata.liangshisq.tcslmon.service.SysParameterService;
import com.stardata.liangshisq.tcslmon.service.TokenService;
import com.stardata.liangshisq.tcslmon.swagger.ApiResponseObject;
import com.stardata.liangshisq.tcslmon.swagger.ApiResponseProperty;
import com.stardata.liangshisq.tcslmon.swagger.ApiResponseSubProperty;
import com.stardata.liangshisq.tcslmon.util.ApiBaseAction;
import com.stardata.liangshisq.tcslmon.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Samson Shu
 * @email shush@stardata.top
 * @date 2020/8/7 21:27
 */
@Api(tags = "登录授权接口")
@RestController
@RequestMapping("/v2/auth/")
@AllArgsConstructor
public class AuthControllerV2 extends ApiBaseAction {
  private final Logger logger = LoggerFactory.getLogger(this.getClass());

  private final DbiEmpService empService;
  private final TokenService tokenService;
  private final SysParameterService parameterService;

  @GetMapping("/shopid")
  @IgnoreAuth
  @ApiOperation(value = "获得系统店铺ID")
  public Object shopid(){
    String shopId = parameterService.getByCode("shopId");
    return success(shopId);
  }

    /**
     * <pre>
     * 后厨PC界面接口
     * </pre>
     */
  @PostMapping("/{shopId}/login")
  @IgnoreAuth
  @ApiOperation(value = "PC登录接口")
  @ApiResponseObject(name="登录后的员工信息",  properties = {
      @ApiResponseProperty(name = "token",description = "登录后令牌",type = "string", example = "vH*;'@A2^hG&-nZI+rM*$uma)8F95/,:"),
      @ApiResponseProperty(name = "user",description = "登录后员工信息",type = "list", properties = {
          @ApiResponseSubProperty(name = "id", description = "员工id", type = "long", example = "1"),
          @ApiResponseSubProperty(name = "code", description = "员工编码", type = "string", example = "401"),
          @ApiResponseSubProperty(name = "name", description = "员工姓名", type = "string", example = "张三"),
      })
  })
  public R login(@PathVariable @ApiParam(name="shopId", value="店铺ID", required = true, example = "59969") Long shopId,
      @RequestBody @ApiParam(name="params", value="登录所需的参数：员工工号、登录密码") LoginVo loginVo){

    if (loginVo == null) {
      return fail(HttpStatus.BAD_REQUEST,"登录失败: 输入为空");
    }
    if(StringUtils.isBlank(loginVo.getEmpNo())) {
      return fail(HttpStatus.BAD_REQUEST,"登录失败: 请输入员工号");
    }
    if(StringUtils.isBlank(loginVo.getPassword())) {
      return fail(HttpStatus.BAD_REQUEST,"登录失败: 请输入密码");
    }

    DbiEmp emp = empService.verifyEmpLogin(shopId, loginVo.getEmpNo(), loginVo.getPassword());
    if (emp == null) {
      return fail(HttpStatus.FORBIDDEN,"工号或密码错误！");
    }

    //创建登录令牌
    Map<String, Object> tokenMap = tokenService.createToken(emp.getId());
    String token = MapUtils.getString(tokenMap, "token");

    if (StringUtils.isBlank(token)) {
      return fail(HttpStatus.INTERNAL_SERVER_ERROR,"登录失败：令牌创建");
    }

    //返回结果给前端
    Map<String, Object> result = new HashMap<>();
    result.put("token", token);
    result.put("user", emp);
    return success(result);
  }

}
